<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/22
 * Time: 9:57
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Db;

class Branch extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //分销列表
    public function branchList(){
        $user_id = input('post.user_id');
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        $data['member'] = Db::name('member_user')->where('pid',$user_id)->select();
        foreach ($data['member'] as $k=>$v){
            $data['member'][$k]['order_number'] = Db::name('order_sku')->where('user_id',$v['user_id'])->count();
            $data['member'][$k]['order_money']  = Db::name('order_sku')->where('user_id',$v['user_id'])->sum('pay_money');
        }
        if (!empty($data)){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }

    //佣金列表
    public function brokerageList(){
        $user_id = input('post.user_id');
        $where = input('post.status')? ['status'=>input('post.status')] : 0;
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        $data['list'] = Db::name('branch_brokerageinfo')->where('pid',$user_id)->where($where)->select();
        foreach ($data['list'] as $k=>$v){
            $data['list'][$k]['user'] = Db::name('member_user')->where('user_id',$v['user_id'])->count();
        }
        if (!empty($data)){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }
    //提现列表
    public function upMoneyList(){
        $user_id = input('post.user_id');
        $where = input('post.status')? ['status'=>input('post.status')] : 0;
        if (!$user_id){
            $this->error('user_id不能为空');
        }
        $data['list'] = Db::name('branch_extract')->where('pid',$user_id)->where($where)->select();
        if (!empty($data)){
            $this->error('无数据');
        }
        $this->success('请求成功',$data);
    }

    //新增佣金
    public function brokerageAdd($user_id,$pid,$order_id,$money){
        $data = [
            'user_id'=>$user_id,
            'pid'=>$pid,
            'order_id'=>$order_id,
            'money'=>$money,
            'time'=>time()
        ];
        $ret = Db::name('branch_brokerageinfo')->insert($data);
        if (!$ret){
            return 0;
        }
        $dsh_money = Db::name('branch_brokerageinfo')->where('pid',$pid)->where('status',0)->sum('money');
        $ret = Db::name('branch_brokerage')->where('user_id',$pid)->find();
        if ($ret){
            $ret = Db::name('branch_brokerage')->where('user_id',$pid)->find();
            $money = $ret['total_money'] + $money;
            $upret = Db::name('branch_brokerage')->where('user_id',$pid)->update(['dsh_money'=>$dsh_money,'total_money'=>$money]);
            if (!$upret){
                return 0;
            }
            return 1;
        }else{
            $insert = Db::name('branch_brokerage')->insert(['user_id'=>$pid,'total_money'=>$dsh_money,'dsh_money'=>$dsh_money]);
            if (!$insert){
                return 0;
            }
            return 1;
        }

    }

    //删除佣金
    public function brokerageDel($user_id,$pid,$order_id,$money){
        $where = [
            'user_id'=>$user_id,
            'pid'=>$pid,
            'order_id'=>$order_id,
        ];
        $ret = Db::name('branch_brokerageinfo')->where($where)->delete();
        $order = Db::name('branch_brokerage')->where( 'user_id',$pid)->find();
        $dsh_money = $order['dsh_money'] - $money;
        $total_money = $order['total_money'] - $money;
        $upret = Db::name('branch_brokerage')->where('user_id',$pid)->update(['dsh_money'=>$dsh_money,'total_money'=>$total_money]);
        if ($ret && $upret){
            return 1;
        }else{
            return 0;
        }
    }

    //佣金修改
    public function brokerageEdit($user_id,$pid,$order_id,$status){
            $where = [
                'user_id'=>$user_id,
                'pid'=>$pid,
                'order_id'=>$order_id,
            ];
            $ret = Db::name('branch_brokerageinfo')->where($where)->update(['status'=>$status]);
            $arr = Db::name('branch_brokerageinfo')->where($where)->find();
            $orde = Db::name('branch_brokerage')->where( 'user_id',$pid)->find();
            $money = Db::name('branch_brokerageinfo')->where('pid',$pid)->where('status',$status)->sum('money');
            if (!$ret){
                return false;
            }
            if ($status == 1){
                $new = $orde['dsh_money'] - $arr['money'];
                $upData = [
                    'wjs_money'=>$money,
                    'dsh_money'=>$new
                ];
            }elseif ($status == 2){
                $new = $orde['dsh_money'] - $arr['money'];
                $upData = [
                    'ktx_money'=>$money,
                    'wjs_money'=>$new
                ];
            }
            $ret = Db::name('branch_brokerage')->where('user_id',$pid)->update($upData);
            if (!$ret){
                return false;
            }
            return true;
    }

    //个人佣金
    public function brokerageInfo(){
        $user_id = input('post.user_id');
        $ret = Db::name('branch_brokerage')->where('user_id',$user_id)->find();
        if (!empty($ret)){
            $this->error('无数据');
        }
        $this->success('请求成功',$ret);
    }

    //申请提现佣金
    public function applyMoney(){
        $user_id = input('post.user_id');
        $money   = input('post.money');
        $where  = Db::name('branch_extract')->where('user_id',$user_id)->where('status',0)->find();
        if (empty($where)){
            $this->error('已有一笔提现待审核，请等待审核通过');
        }
        $where  = Db::name('branch_brokerage')->where('user_id',$user_id)->find();
        if ($money > $where['ktx_money']){
            $this->error('提现金额大于可提现金额');
        }
        $insert = [
            'user_id'=>$user_id,
            'money'  =>$money,
            'status' =>0,
            'time'   =>time()
        ];
        $insert = Db::name('branch_extract')->insert($insert);
        if (!$insert){
            $this->error('请求失败');
        }
        $this->success('请求成功');
    }

}